import { Controller } from 'egg';

class DepartmentController extends Controller {
    /**
     * 创建部门
     */
    public async createDepartment() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                department_name: 'string',
                admin_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.departmentService.createDepartment(request.body);
        ctx.body = ctx.helper.responseSuccess('创建成功');
    }

    /**
     * 删除部门
     */
    public async deleteDepartment() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                department_id: 'string',
                admin_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.departmentService.deleteDepartment(request.body);
        ctx.body = ctx.helper.responseSuccess('删除成功');
    }

    /**
     * 查找部门
     */
    public async searchDepartment() {
        const { ctx } = this;
        const result = await ctx.service.departmentService.searchDepartment();
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 根据部门id查找部门信息
     */
    public async searchDepartmentById() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                department_id: 'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        const result = await ctx.service.departmentService.searchDepartmentById(request.body);
        ctx.body = ctx.helper.responseSuccess('查找成功', result);
    }

    /**
     * 修改部门
     */
    public async updateDepartment() {
        const { ctx, ctx: { request } } = this;
        try {
            ctx.validate({
                admin_id:'string',
                department_id: 'string',
                department_name:'string'
            }, request.body)
        } catch (e) {
            ctx.body = ctx.helper.responseErrorParam(e.errors[0].message);
            return;
        }
        await ctx.service.departmentService.updateDepartment(request.body);
        ctx.body = ctx.helper.responseSuccess('修改成功');
    }
}
export default DepartmentController;